<?php
declare (strict_types=1);

namespace app\admin\model\base;

use app\BaseModel;

/**
 * 用户企业微信关注记录表
 */
class UserSubscribeWorkWx extends BaseModel
{

    /**
     * 用户对公众号关注情况记录
     * @param string $unionid
     * @param int $subscribe
     * @return bool
     */
    public static function oneLogSave(string $unionid,int $subscribe): bool
    {
        $where = ['unionid' => $unionid,'subscribe'=>1];
        $res = self::findOne($where);
        $where['subscribe'] = $subscribe;
//        if (!$res && $subscribe==0) {
//            $where['subscribe'] = 3;
//        }
        //查询日志是否存在
        $where['unionid'] = $unionid;
        $where['create_time'] = time();
        $model = new self();
        $model->save($where);
        return true;
    }

    /**
     * 判断用户是否关注企业微信
     * @param string $unionid
     * @return bool
     */
    public static function itSubscribe(string $unionid): bool
    {
        //查询日志是否存在
        $where[] = ['unionid','=',$unionid];
        $res = self::order('id desc')->where($where)->find();
        $res = $res ? $res->toArray() : [];
        if ($res && $res['subscribe']==1) {
            return true;
        }
        return false;
    }
}




